﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GOFconsole
{
    public class Chapter8_Factory
    {
        public void FactoryTest()
        { 
            //简单工厂实现方式
            LeiFeng stuA = SimpleFactory.CreateLeiFeng("学雷锋的大学生");
            stuA.BuyRice();
            LeiFeng volA = SimpleFactory.CreateLeiFeng("社区志愿者");
            volA.Wash();

            //工厂模式的实现
            IFactory factory = new UnderGraduateFactory();  //换社区志愿者只要修改工厂就可以
            LeiFeng stuFA = factory.CreateLeiFeng();
            stuFA.BuyRice();


        }

    }

    //雷锋类
    class LeiFeng
    {
        public void Sweep()
        {
            Console.WriteLine("扫地");
        }
        public void Wash()
        {
            Console.WriteLine("洗碗");
        }
        public void BuyRice()
        {
            Console.WriteLine("买米");
        }
    }

    //学雷锋的大学生
    class UnderGraduate : LeiFeng
    { }
    //长期帮忙的社区志愿者
    class Volunteer : LeiFeng
    { }
    //简单工厂实现的方式
    class SimpleFactory
    {
        public static LeiFeng CreateLeiFeng(string type)
        { 
            LeiFeng result=null;
            switch (type)
            { 
                case "学雷锋的大学生":
                    result = new UnderGraduate();
                    break;
                case "社区志愿者":
                    result = new Volunteer();
                    break;
            }
            return result;
        }
    }

    //工厂模式的实现方式
    //雷锋工厂
    interface IFactory
    {
        LeiFeng CreateLeiFeng();
    }
    //学雷锋的大学生工厂
    class UnderGraduateFactory : IFactory
    { 
        public LeiFeng CreateLeiFeng()
        {
            return new UnderGraduate();
        }
    }
    //社区志愿者工厂
    class VolunteerFactory : IFactory
    {
        public LeiFeng CreateLeiFeng()
        {
            return new Volunteer();
        }
    }

}
